/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.UI;

import com.usoog.tdcore.UserInfo;
import java.awt.Component;
import java.util.WeakHashMap;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class PlayerListRenderer implements ListCellRenderer {

	private static WeakHashMap<UserInfo, PlayerListPanel> panelCache = new WeakHashMap<UserInfo, PlayerListPanel>();
	private boolean longFormat = true;

	public PlayerListRenderer() {
	}

	// This is the only method defined by ListCellRenderer.
	// We just reconfigure the JLabel each time we're called.
	public Component getListCellRendererComponent(
			JList list, // the list
			Object value, // value to display
			int index, // cell index
			boolean isSelected, // is the cell selected
			boolean cellHasFocus) // does the cell have focus
	{
		if (value instanceof UserInfo) {
			UserInfo info = (UserInfo) value;
			PlayerListPanel panel = panelCache.get(info);
			if (panel == null) {
				panel = new PlayerListPanel(info);
				panelCache.put(info, panel);
			}
			panel.setSelected(isSelected);
			panel.setLongFormat(longFormat);
			return panel;
		}
		return new JLabel("Unknown item");
	}

	public boolean isLongFormat() {
		return longFormat;
	}

	public void setLongFormat(boolean longFormat) {
		this.longFormat = longFormat;
	}
}

